import 'package:flutter/material.dart';
import 'package:date_format/date_format.dart';
import 'package:flutter_cupertino_date_picker/flutter_cupertino_date_picker.dart';

class CupertionDatePickerPage extends StatefulWidget {
  @override
  _CupertionDatePickerPageState createState() => _CupertionDatePickerPageState();
}

class _CupertionDatePickerPageState extends State<CupertionDatePickerPage> {

  DateTime _dateTime = DateTime.now();
  static String MIN_DATETIME = '2010-05-12';
  static String MAX_DATETIME = '2020-05-12';
  String _format = 'yyyy-MMMM-dd';
  void _showCupertinoDatePicker() {

    DatePicker.showDatePicker(
      context,
      pickerTheme: DateTimePickerTheme(
        showTitle: true,
        confirm: Text('确定', style: TextStyle(color: Colors.red)),
        cancel: Text('取消', style: TextStyle(color: Colors.cyan)),
      ),
      minDateTime: DateTime.parse(MIN_DATETIME),
      maxDateTime: DateTime.parse(MAX_DATETIME),
      initialDateTime: _dateTime,
      dateFormat: 'yyyy年M月d日    EEE,H时:m分',
      locale: DateTimePickerLocale.zh_cn,
      onClose: () => print("----- onClose -----"),
      onCancel: () => print('onCancel'),
      pickerMode: DateTimePickerMode.datetime,
      /*onChange: (dateTime, List<int> index) {
        setState(() {
          _dateTime = dateTime;
        });
      },*/
      onConfirm: (dateTime, List<int> index) {
        setState(() {
          _dateTime = dateTime;
        });
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('第三方日期组件'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          InkWell( // 可以点击的组件
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text('${formatDate(_dateTime, [yyyy,'年', mm, '月', dd, '日 ', HH,':', nn])}'),
                Icon(Icons.arrow_drop_down)
              ],
            ),
            onTap: this._showCupertinoDatePicker,
          ),
        ],
      ),
    );
  }
}
